package com.shop.controller;

import com.shop.domain.User;
import com.shop.entity.Result;
import com.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/User")
public class UserController {
    @Autowired
    private UserService iUserService;

    /**
     * 查询全部用户表
     * @param
     * @return
     */
    @GetMapping("/selectAll")
    public Result<List<User>> selectAll(){
        return new Result<>("查询成功",iUserService.selectAll());
    }

    /**
     * 新增用户表
     * @param iUser
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody User iUser){

       Integer i =  iUserService.insert(iUser);
       if(i==-2){
           return Result.fail("用户已存在");
       }
        return new Result<>("新增成功");
    }
    /**
     * 修改用户表
     * @param iUser
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody User iUser){
        iUserService.update(iUser);
        return new Result<>("修改成功");
    }
    /**
     * 删除用户表
     * @param ids
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam String ids){
        iUserService.delete(ids);
        return new Result<>("删除成功");
    }

    /**
     * 条件查询用户表
     * @param iUser
     * @return
     */
    @PostMapping("/selectList")
    public Result< List<User> > select(@RequestBody User iUser){
        return new Result<>("条件查询成功",iUserService.selectList(iUser));
    }
    /**
     * 启动、
     * 停用
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/status")
    public Result updateStatus(@RequestParam Long id,@RequestParam Integer status){
        iUserService.status(id,status);
        return new Result("修改成功");
    }

    /**
     * 重置密码
     */
    @PostMapping("/reset")
    public Result reset(@RequestBody Long id){
        iUserService.resetPwd(id);
        return new Result("修改成功");
    }

    /**
     * 登录
     *
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        String login = iUserService.login(user.getUsername(), user.getPassword());
        if (login.equals("-1")) {
            return Result.fail("用户名或密码错误");
        } else if (login.equals("-2")) {
            return Result.fail("该用户不存在");
        } else if (login.equals("-3")) {
            return Result.fail("该用户已停用");
        }
        return new Result("登录成功",login);
    }

}
